package eu.whowie.jast.server.web;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import eu.whowie.jast.remote.UIRemoteInterface;
import eu.whowie.jast.remote.YastServer;
import eu.whowie.jast.server.ContextChangeListener;
import eu.whowie.jast.server.JastRmiServer;

public class ApplicationContext implements UIRemoteInterface {
	private static final ApplicationContext context = new ApplicationContext();
	private LinkedList<ContextChangeListener> listeners = new LinkedList<ContextChangeListener>();
	private List<YastServer> connectedServers = new ArrayList<YastServer>();
	private Logger logger = Logger.getLogger("APPLICATION_CONTEXT");
	private JastRmiServer jastRmiServer;

	private ApplicationContext() {
	}

	public static ApplicationContext getContext() {
		return context;
	}

	public void addContextChangeListener(ContextChangeListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void ping(String value) throws RemoteException {
		logger.log(Level.INFO, "get ping: " + value);
	}

	@Override
	public void connect(YastServer server) {
		this.connectedServers.add(server);
		for (ContextChangeListener item : listeners) {
			item.serverConnected(server);

		}

	}

	@Override
	public void disconnect(YastServer server) {
		this.connectedServers.remove(server);
		for (ContextChangeListener item : listeners) {
			item.serverDisconnected(server);

		}

	}

	public List<YastServer> getConnectedServers() {
		return connectedServers;
	}

	/**
	 * @param jastRmiServer
	 */
	public void setRmiSErver(JastRmiServer jastRmiServer) {
		this.jastRmiServer = jastRmiServer;

	}

	/**
	 * @return the jastRmiServer
	 */
	public JastRmiServer getJastRmiServer() {
		return jastRmiServer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.whowie.jast.remote.UIRemoteInterface#ping(long)
	 */
	@Override
	public long ping(long id) {
		return id;
	}
}
